package cn.diancan.model.types;

import java.util.Random;

import cn.diancan.util.DoubleArith;

public enum TranType {
    //交易分类   1.充值  2.提现 3.订单平台服务费
    
    //加钱扣钱是相对于系统钱包的.
    /**
     * 充值
     */
    RECHARGE			(1,"充值",TranUseType.ADDMONEY,0f,false,false,false), 
    /**
     * 提现  (*重要字段)
     */
    CHANGECASH			(2,"提现",TranUseType.SUBNONEY,0f,true,false,false),
    
    /**
     * 订单(UserOrder) 平台服务费
     */
    USERORDERAGENCYFEES		(3,"订单平台服务费",TranUseType.SUBNONEY,0f,0.4f,0.85f,false,false,false),
    
    /**
     * 首页置顶
     */
    USERINFOINDEXTOP		(4,"个人信息首页置顶",TranUseType.SUBNONEY,0f,false,false,false),
    
    /**
     * 下级消费分成
     */
    LOWERCONSUME		(5,"下级消费分成",TranUseType.ADDMONEY,0f,true,false,true),
    
    /**
     * 技能服务订单
     */
    USERORDERSKILLSERVE		(6,"技能服务订单付款",TranUseType.SUBNONEY,0f,false,false,false);
    
    
    private Integer tranType; 
    private String description;
    private Integer useType; //1.加钱     2.扣钱  (以钱包里面的钱为准)
    private Float feeRates;  //手续费 
    private Float fewardMaxRates;
    private Float fewardMinRates;
    private boolean isChangeWallet;
    private boolean isUser2UserTran;//是不是用户直接交易，不用平台中介. 
    private boolean isOperatingGainUser;//是否操作收款人的   false =操作付款人  true =操作收款人  (分成，系统奖励==true) 
    
    
    private TranType(Integer tranType,String description,Integer useType,Float feeRates,boolean isChangeWallet ,boolean isUser2UserTran,boolean isOperatingGainUser) {
	this(tranType, description, useType, feeRates, 0f, 0f, isChangeWallet, isUser2UserTran,isOperatingGainUser);
    }
    
    private TranType(Integer tranType,String description,Integer useType,Float feeRates,Float fewardMaxRates,Float fewardMinRates,boolean isChangeWallet ,boolean isUser2UserTran,boolean isOperatingGainUser) {
	this.tranType =tranType; 
	this.description=description;
	this.useType =useType;
	this.feeRates =feeRates;
	if(feeRates<0 || feeRates>1){
	    this.feeRates =0f;
	}
	this.fewardMaxRates =fewardMaxRates;
	this.fewardMinRates = fewardMinRates;
	this.isChangeWallet =isChangeWallet;
	this.isUser2UserTran =isUser2UserTran;
	this.isOperatingGainUser =isOperatingGainUser;
    }
    
    public static TranType findTranType(Integer tranType){
	if(null ==tranType){
	    return null;
	}
	TranType t[]= TranType.values();
	for (int i = 0; i < t.length; i++) {
	    if(t[i].tranType == tranType ){
		return t[i];
	    }
	}
	return null;
    }
    
    
    public static void main(String[] args) { 
	for (int i = 0; i <100; i++) {
	    TranType enums = TranType.USERORDERAGENCYFEES;
		System.out.println(enums.getFeeRates());
	}
    }
    
    /**
     * 检查值是否存在
     * @param type
     * @return
     */
    public static boolean checkExists(Integer type){
	if(null==type){
	    return false;
	}
	TranType enums[] =TranType.values();
	for (int i = 0; i < enums.length; i++) {
	    TranType singleEnum=  enums[i];
	    if(singleEnum.getTranType().equals(type)){
		return true;
	    }
	}
	return false;
    }
    
    public boolean getIsChangeWallet(){
	return isChangeWallet;
    }
    

    public Integer getTranType() {
        return tranType;
    }

    public Integer getUseType() {
        return useType;
    }

    public Float getFeeRates() {
	if(this.fewardMaxRates.equals(0f) && this.fewardMinRates.equals(0f)){
	    return feeRates;
	} 
        return getFewardRates();
    }

    public boolean getIsUser2UserTran() {
        return isUser2UserTran;
    }

    public String getDescription() {
        return description;
    }  
    
    public boolean getIsOperatingGainUser() {
        return isOperatingGainUser;
    }
 

    /**
     * 返回随机奖励值
     * @return
     */
    public Float getFewardRates(){ 
	Float f ;
	while(true){
	    f = new Random().nextFloat();
	    if( f>=this.fewardMaxRates &&  f<=this.fewardMinRates){
		break;
	    }
	}
	return ((Double)DoubleArith.round( f, 1)).floatValue();
    }
    
}
